package net.gz01.wallet.web.controller.admin;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import net.gz01.util.ZODate;
import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.Channel;
import net.gz01.wallet.web.model.TradeConfig;

public class ChannelController extends BaseController{

	public void search(){
		render("basedata/channelList.html");
	}

	public void listJson() {
		ZOSearchCondition sc = super.getSearchCondition();
		Page<Channel> pageResult = Channel.dao.search(sc);
		renderJson(new PageRespon(pageResult));
	}

	public void addPage(){
		render("basedata/channelAdd.html");
	}

	public void updatePage(){
		Channel channel = Channel.dao.findById(getPara("id"));
		setAttr("channel", channel);
		render("basedata/channelUpdate.html");
	}

	@Before(Tx.class)
	public void add(){
		Channel channel = getModel(Channel.class);
		if(Channel.dao.selectByCode(channel.getCode()) != null){
			renderFail("渠道号已存在");
		}else {
			channel.setUpdateTime(ZODate.getDateTime());
			channel.save();
			renderSuccess();
		}
	}

	@Before(Tx.class)
	public void update(){
		Channel channel = getModel(Channel.class);
		channel.setUpdateTime(ZODate.getDateTime());
		channel.update();
		renderSuccess();
	}

	@Before(Tx.class)
	public void delete(){
		if(TradeConfig.dao.isExistByChannelCode(getPara("code"))){
			String msg = "该渠道已被引用，如需删除，请前往基础配置-交易配置更新交易规则！";
			renderSuccess(msg,false);
		}else {
			Channel channel = Channel.dao.findById(getPara("id"));
			Channel.dao.deleteById(channel.getId());
			renderSuccess(false);
		}
	}

}
